﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RacingDog
{
    public partial class Form1 : Form
    {
        private Guy[] _guys = new Guy[3];

        private Greyhound[] _greyhoundArray = new Greyhound[4];

        private Random _myRandom = new Random();

        public Form1()
        {
            InitializeComponent();

            LblMinimumBet.Text = @"Minimum bet: " + NudBetsAmount.Minimum + @" bucks";

            _guys[0] = new Guy
            {
                Name = "Joe",
                Cash = 50,
                MyRadioButton = RbtnJoe,
                MyLabel = LblJoe
            };
            _guys[1] = new Guy
            {
                Name = "Bob",
                Cash = 75,
                MyRadioButton = RbtnBob,
                MyLabel = LblBoe
            };
            _guys[2] = new Guy
            {
                Name = "Al",
                Cash = 45,
                MyRadioButton = RbtnAl,
                MyLabel = LblAl
            };

            foreach (var guy in _guys)
            {
                guy.PlaceBet(0, 0);
                guy.UpdateLabels();
            }
        }

        private void InitializeGreyhounds()
        {
            _greyhoundArray[0] = CreateGreyhound(Pic1);
            _greyhoundArray[1] = CreateGreyhound(Pic2);
            _greyhoundArray[2] = CreateGreyhound(Pic3);
            _greyhoundArray[3] = CreateGreyhound(Pic4);
        }

        private Greyhound CreateGreyhound(PictureBox pic)
        {
            pic.Left = 32;
            return new Greyhound(pic, pic.Left, PicRacetrack.Width - pic.Width, _myRandom);
        }

        private void BtnRace_Click(object sender, EventArgs e)
        {
            InitializeGreyhounds();
            GrpRace.Enabled = false;
            TmrRace.Start();
        }

        private void TmrRace_Tick(object sender, EventArgs e)
        {
            for (var i = 0; i < 4; i++)
            {
                if (_greyhoundArray[i].Run())
                {
                    TmrRace.Stop();
                    GrpRace.Enabled = true;
                    MessageBox.Show(@"#" + (i + 1) + @" dog win.");
                    foreach (var guy in _guys)
                    {
                        guy.Collect(i);
                    }
                }
            }
        }

        private void RbtnJoe_CheckedChanged(object sender, EventArgs e)
        {
            LblBettor.Text = @"Joe";
        }

        private void RbtnBob_CheckedChanged(object sender, EventArgs e)
        {
            LblBettor.Text = @"Bob";
        }

        private void RbtnAl_CheckedChanged(object sender, EventArgs e)
        {
            LblBettor.Text = @"Al";
        }

        private void BtnBets_Click(object sender, EventArgs e)
        {
            var bettor = _guys.Single(g => g.Name == LblBettor.Text);
            bettor.PlaceBet((int)NudBetsAmount.Value, (int)NudBetsDog.Value);
        }
    }

    public class Guy
    {
        public string Name;

        public Bet MyBet;

        public int Cash;

        public RadioButton MyRadioButton;

        public Label MyLabel;

        public void UpdateLabels()
        {
            MyLabel.Text = MyBet.GetDescription();
            MyRadioButton.Text = Name + @" has " + Cash + @" bucks";
        }

        public void ClearBet()
        {
            MyBet = new Bet(0, 0, this);
        }

        public bool PlaceBet(int betAmount, int dogToWin)
        {
            if (Cash >= betAmount)
            {
                MyBet = new Bet(betAmount, dogToWin, this);
                UpdateLabels();
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Collect(int winnner)
        {
            Cash += MyBet.PayOut(winnner);
            ClearBet();
            UpdateLabels();
        }
    }

    public class Bet
    {
        public int Amount;

        public int Dog;

        public Guy Bettor;

        public Bet(int amount, int dog, Guy bettor)
        {
            Amount = amount;
            Dog = dog;
            Bettor = bettor;
        }

        public string GetDescription()
        {
            if (Amount == 0)
            {
                return Bettor.Name + " hasn't placed a bet";
            }

            return Bettor.Name + " bets " + Amount + " on dog #" + Dog;
        }

        public int PayOut(int winner)
        {
            if (Dog == winner + 1)
            {
                return Amount;
            }
            else
            {
                return -Amount;
            }
        }
    }

    public class Greyhound
    {
        public PictureBox MyPictureBox;

        /// <summary>
        /// Where my PictureBox starts.
        /// </summary>
        public int StartingPosition;

        /// <summary>
        /// How long the racetrack is.
        /// </summary>
        public int RacetrackLength;

        public int Location;

        public Random Randomizer;

        public Greyhound(PictureBox myPictureBox, int startingPosition, int racetrackLength, Random randomizer)
        {
            MyPictureBox = myPictureBox;
            StartingPosition = startingPosition;
            RacetrackLength = racetrackLength;
            Randomizer = randomizer;
        }

        public bool Run()
        {
            Location += Randomizer.Next(1, 4);

            MyPictureBox.Left = StartingPosition + Location;

            return StartingPosition + Location > RacetrackLength;
        }

        public void TakeStartingPosition()
        {
            Location = 0;
            MyPictureBox.Left = StartingPosition;
        }
    }
}
